/**
 * 
 */
package br.com.unifebe.tcc.componente.GUI;

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.KeyboardFocusManager;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;

/**
 * @Autor Marcelo Leite
 *
 */
public class JMLComboBox extends JComboBox{
	private Object[] values;
	private String atributo;
	private JLabel msgLabel;
	private int valorPadrao;
	/**
	 * 
	 */
	public JMLComboBox() {
		super();
		init();
	}

	/**
	 * @param aModel
	 */
	public JMLComboBox(ComboBoxModel aModel) {
		super(aModel);
		init();
	}

	/**
	 * @param items
	 */
	public JMLComboBox(Object ... items) {
		super(items);
		setValues(items);
		init();
	}

	/**
	 * @param items
	 */
	public JMLComboBox(Vector<?> items) {
		super(items);
		setValues( items.toArray() );
		init();
	}
	
	public JMLComboBox(Object[] items,Object[] values) {
		super(items);
		setValues(values);
		init();
	}
	
	public JMLComboBox(Object[][] items) {
		super();
		String[] valores = new String[items.length];
		
		for (int i=0;i<items.length;i++) {
			this.addItem(items[i][0]);
			valores[i] = String.valueOf(items[i][1]);
		}
		this.setValues(valores);
		init();
	}
	
	
	
	protected void init() {
		this.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode()==10) {
					((JComponent)findNextFocus()).grabFocus();
				}
			}
			@Override
			public void keyReleased(KeyEvent e) {
			}
			@Override
			public void keyTyped(KeyEvent e) {
			}
		});
		
		this.addFocusListener(new java.awt.event.FocusListener() {
			@Override
			public void focusGained(java.awt.event.FocusEvent e) {
//				setBackground( Parametros.getCorEditSelecionado() );
//				setFont( Parametros.getFontEditSelecionado() );
				if (JMLComboBox.this.msgLabel!=null) {
					JMLComboBox.this.msgLabel.setText(JMLComboBox.this.getToolTipText());
				}
			}
			@Override
			public void focusLost(java.awt.event.FocusEvent e) {
//				setBackground( Parametros.getCorEdit() );
//				setFont( Parametros.getFontEdit() );
				if (JMLComboBox.this.msgLabel!=null) {
					JMLComboBox.this.msgLabel.setText("");
				}
			}
		});
		
		
		
		addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					setToolTipText( e.getItem()+"" );
				}
			}
		});
		
		setToolTipText( getSelectedItem()+"" );
		
		setRenderer( new ComboTooltipRenderer() );
		
	}
	public class ComboTooltipRenderer extends DefaultListCellRenderer {
		
		@Override
		public Component getListCellRendererComponent(JList list, Object
				value, int index, boolean isSelected, boolean cellHasFocus) {
			
			JComponent comp = (JComponent)
			
			super.getListCellRendererComponent(list, value, index, isSelected,
					cellHasFocus);

			if (value != null) {
				comp.setToolTipText(String.valueOf(value) );
			} else {
				comp.setToolTipText(null);
			}
			
			return comp;
			
		}
	}
	public void setValues(Object[] values) {
		this.values = values;
	}

	public Object[] getValues() {
		return this.values;
	}
	
	public Object getSelectedValue() {
		return this.values == null  ? getItemAt( getSelectedIndex() ) : this.values[ getSelectedIndex() ];
	}

	public void setAtributo(String s) {
		this.atributo = s;
	}
	public String getAtributo() {
		return this.atributo;
	}
	
	public void setMsgLabel(JLabel label) {
		this.msgLabel = label;
	}
	
	public JLabel getMsLabel() {
		return this.msgLabel;
	}
	
	public int getIndexOfValue(Object value) {
		for(int i=0;i<this.values.length;i++) {
			if (values[i].equals(value)) {
				return i;
			}
		}
		return -1;
	}
	
	public Component findNextFocus() {
        // Find focus owner
        Component c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        Container root = c == null ? null : c.getFocusCycleRootAncestor();
    
        if (root != null) {
            FocusTraversalPolicy policy = root.getFocusTraversalPolicy();
            Component nextFocus = policy.getComponentAfter(root, c);
            if (nextFocus == null) {
                nextFocus = policy.getDefaultComponent(root);
            }
            return nextFocus;
        }
        return null;
    }

	public Object getUserObject() {
		return getSelectedValue();
	}

	public boolean isEnabledControl() {
		return isEnabled();
	}

	public void reset() {
		setSelectedIndex( getValorPadrao() );
	}

	public void setEnabledControl(boolean b) {
		this.setEnabled(b);
	}

	public void setUserObject(Object object) {
	}

	public void setValorPadrao(int valorPadrao) {
		this.valorPadrao = valorPadrao;
	}
	
	public int getValorPadrao() {
		return valorPadrao;
	}
	public void initializing(boolean state) {
		// TODO Auto-generated method stub
		
	}}
